FACELANDMARKS API Endpoint

Version 2.197 (Release Notes ↗)

Description

Utilize the FACELANDMARKS API endpoint to detect and extract facial landmarks, along with their corresponding rectangle coordinates. This functionality can be integrated with other API endpoints, including CROP, MOGRIFY, and MERGE, to facilitate advanced face processing. The output will include the rectangle coordinates and extracted facial shapes for each detected face. Several complementary API endpoints are available, such as crop for face extraction, drawrectangles for face annotation, and mogrify for content redaction.

HTTP Methods

GET, POST

HTTP Parameters

Required

Fields Type Description
img URL Input media URL. If you want to upload your image directly from your app, then submit a multipart/form-data POST request.
key String Your PixLab API Key ↗. You can also embed your key in the WWW-Authenticate: HTTP header and omit this parameter if you want to.

POST Request Body

If you plan to use POST instead of a simple GET request:

Allowed Content-Types:

multipart/form-data
application/json

Use multipart/form-data if you want to upload your media file directly (refer to the REST API code samples or The PixLab Github Repository↗ for a working example). If you are using JSON, then the media file must be already uploaded somewhere. Call store if you want to upload an image before invoking this endpoint.

HTTP Response

application/json.

This command always return a JSON object after each call. The field of interest here is the faces array which hold the rectangle & landmarks coordinates for each detected face. The following are the JSON fields returned in response body:
Fields Type Description
status Integer Status code 200 indicates success, any other code indicates failure.
faces Array JSON array holding the rectangle & landmarks coordinates (See below for fields).
error String Error message if status != 200.


The following are the object fields returned for each detected face in the faces array defined above:
Fields Type Description
rectangle Array JSON array holding the rectangle coordinates for this face (See below).
landmarks Array JSON array holding the landmarks coordinates for this face (See below).

The following are the object fields returned in each rectangle array, member of the faces array defined above:
Fields Type Description
face_id Integer Detected face ID.
left Integer Rectangle left coordinate: X.
top Integer Rectangle top coordinate: Y.
width Integer Rectangle width.
height Integer Rectangle height.

The following are the object fields returned in each landmarks array, member of the faces array defined above:
Fields Type Description
nose Array Nose Tip coordinates in the form [x: Integer, y: Integer].
nose_bottom_left Array Nose bottom left coordinates in the form [x: Integer, y: Integer].
nose_bottom_right Array Nose bottom right coordinates in the form [x: Integer, y: Integer].
mouth_left Array left most coordinates of the mouth in the form [x: Integer, y: Integer].
mouth_right Array Right most coordinates of the mouth in the form [x: Integer, y: Integer].
bottom_lip Array Bottom Lip coordinates in the form [x: Integer, y: Integer].
top_lip Array Top Lip coordinates in the form [x: Integer, y: Integer].
chin Array Chin coordinates in the form [x: Integer, y: Integer].
eye Array Array holding the coordinates of the region surrounding the eyes. These includes:
  • pupil_left: Left Pupil coordinates in the form [x: Integer, y: Integer].
  • pupil_right: Right Pupil coordinates in the form [x: Integer, y: Integer].
  • left_brow_inner: Left Eye Brow Inner coordinates in the form [x: Integer, y: Integer].
  • left_brow_outer: Left Eye Brow Outer coordinates in the form [x: Integer, y: Integer].
  • right_brow_inner: Right Eye Brow Inner coordinates in the form [x: Integer, y: Integer].
  • right_brow_outer: Right Eye Brow Outer coordinates in the form [x: Integer, y: Integer].
  • left_inner: Left Eye Inner coordinates in the form [x: Integer, y: Integer].
  • left_outer: Left Eye Outer coordinates in the form [x: Integer, y: Integer].
  • right_inner: Right Eyer Inner coordinates in the form [x: Integer, y: Integer].
  • right_outer: Right Eyer Outer coordinates in the form [x: Integer, y: Integer].
bone Array Array holding the coordinates of some regions of the bone. These includes:
  • center: Bone Center coordinates in the form [x: Integer, y: Integer].
  • outer_left: Coordinates of Left most region of the bone in the form [x: Integer, y: Integer].
  • outer_right: Coordinates of Right most region of the bone in the form [x: Integer, y: Integer].


  • For additional processing, feel free to adjust these coordinates for the next command such as crop for face extraction, drawrectangles for marking faces, mogrify and merge to mimic the Snapchat filters effects (refer to the REST API code samples for a concrete example).

  • If you need additional landmarks coordinates, please fill an API feature request ticket from the support tab of your dashboard ↗.

Code Samples


import requests
import json
from typing import List, Dict, Any

def smart_resize(img: str, width: int, height: int, key: str) -> str:
    """Resize an image using smartresize."""
    params = {
        'img': img,
        'key': key,
        'width': width,
        'height': height
    }
    response = requests.get('https://api.pixlab.io/smartresize', params=params)
    response.raise_for_status()
    reply = response.json()
    if reply['status'] != 200:
        raise Exception(reply['error'])
    return reply['link']

def apply_snapchat_filters(
    img: str,
    key: str,
    draw_crown: bool = False,
    flower: str = 'http://pixlab.xyz/images/flower_crown.png',
    dog_left_ear: str = 'http://pixlab.xyz/images/dog_left_ear.png',
    dog_right_ear: str = 'http://pixlab.xyz/images/dog_right_ear.png',
    dog_nose: str = 'http://pixlab.xyz/images/dog_nose.png'
) -> str:
    """Apply Snapchat-like filters to an image."""
    # Detect face landmarks
    response = requests.get(
        'https://api.pixlab.io/facelandmarks',
        params={'img': img, 'key': key}
    )
    response.raise_for_status()
    reply = response.json()
    
    if reply['status'] != 200:
        raise Exception(reply['error'])
    
    faces = reply.get('faces', [])
    if not faces:
        raise Exception("No faces were detected")
    
    print(f"{len(faces)} faces were detected")
    coordinates: List[Dict[str, Any]] = []

    for face in faces:
        cord = face['rectangle']
        landmarks = face['landmarks']
        
        if draw_crown:
            fit_crown = smart_resize(
                flower,
                20 + cord['width'],
                0,
                key
            )
            coordinates.append({
                'img': fit_crown,
                'x': landmarks['bone']['outer_left']['x'],
                'y': landmarks['bone']['outer_left']['y']
            })
        else:
            coordinates.extend([
                {
                    'img': smart_resize(dog_left_ear, cord['width']//2, cord['height']//2, key),
                    'x': landmarks['bone']['outer_left']['x'],
                    'y': landmarks['bone']['outer_left']['y']
                },
                {
                    'img': smart_resize(dog_right_ear, cord['width']//2, cord['height']//2, key),
                    'x': landmarks['bone']['outer_right']['x'],
                    'y': landmarks['bone']['outer_right']['y']
                },
                {
                    'img': smart_resize(dog_nose, cord['width']//2, cord['height']//2, key),
                    'x': landmarks['nose']['x'] - 18,
                    'y': landmarks['nose']['y'] - 10
                }
            ])
        draw_crown = not draw_crown

    # Perform composite operation
    response = requests.post(
        'https://api.pixlab.io/merge',
        headers={'Content-Type': 'application/json'},
        data=json.dumps({
            'src': img,
            'key': key,
            'cord': coordinates
        })
    )
    response.raise_for_status()
    reply = response.json()
    
    if reply['status'] != 200:
        raise Exception(reply['error'])
    
    return reply['link']

if __name__ == "__main__":
    # Configuration
    IMG_URL = 'https://trekkerscrapbook.files.wordpress.com/2013/09/face-08.jpg'
    API_KEY = 'My_PIXLAB_API_KEY'
    DRAW_CROWN = False

    try:
        result_url = apply_snapchat_filters(IMG_URL, API_KEY, DRAW_CROWN)
        print(f"Snap Filter Effect: {result_url}")
    except Exception as e:
        print(f"Error: {e}")
← Return to API Endpoint Listing